package com.itqf.controller;

import com.itqf.execption.UserLoginExecption;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * projectName: springmvcgp8
 *
 * @author: 赵伟风
 * time: 2021/8/6 16:30
 * description:
 */

/**
 * 全局异常处理增强
 *   注意：必须被扫描
 */
@ControllerAdvice
public class AllExecptionHandler {

    /**
     * 异常处理的handler
     *    @ExceptionHandler(异常.class)
     *    handler正常handler
     * @param e  发生异常的对象
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public  Object execptionHandler(Exception e){

        Map<String,Object> map = new HashMap<>();
        map.put("error", 300);
        map.put("message", e.getMessage());

        return  map;
    }


    /**
     * 异常处理的handler
     *    @ExceptionHandler(异常.class)
     *    handler正常handler
     * @param e  发生异常的对象
     * @return
     */
    @ExceptionHandler(UserLoginExecption.class)
    @ResponseBody
    public  Object execptionHandler(UserLoginExecption e){

        Map<String,Object> map = new HashMap<>();
        map.put("code", 300);
        map.put("message", e.getMessage());

        return  map;
    }

}
